package com.xiweicheng.test.springboot.jwt;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.impl.TextCodec;

public class JwtMain {

	public static void main(String[] args) {
		String secret = "P@assw0rd";
		String base64EncodedSecretKey = TextCodec.BASE64.encode(secret);

		// 生成jwt token
		String compactJws = Jwts.builder().setSubject("Joe").signWith(SignatureAlgorithm.HS512, base64EncodedSecretKey)
				.compact();
		System.out.println(compactJws);
		System.out.println(Jwts.parser().setSigningKey(base64EncodedSecretKey).parseClaimsJws(compactJws).getBody());

		// 解析jwt token
		String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE1MzAzMjUyNDh9.5pDrkBVV-eBMPJ5os3zcnXkxxLQA2GwQfHMDP0NnvsM";
		System.out.println(Jwts.parser().setSigningKey(base64EncodedSecretKey).parseClaimsJws(token).getBody());
	}

}
